# -*- coding: utf-8 -*-

from typing import List
from fastapi import Query
from collections import namedtuple

PageFilter = namedtuple("PageFilter", ["order_by", "filter", "like", "in_"])

def get_page_filter(order_by: List[str]=Query(default=["id=true"], description="example: id=true, name=false")
        , filter: List[str]=Query(default=None, description="example: name=jack")
        , like: List[str]=Query(default=None, description="example: name=jack")
        , in_: List[str]=Query(default=None, description="example: id=1,2,3"))->PageFilter:
    order_by = {key: (value.lower() == 'true') for key, value in (item.split("=") for item in order_by if "=" in item)}
    filter = dict(item.split("=") for item in filter if "=" in item) if filter else {}
    like = dict(item.split("=") for item in like if "=" in item) if like else {}
    in_ = {key: value.split(",") for key, value in (item.split("=") for item in in_ if "=" in item)} if in_ else {}
    # return {"order_by": order_by, "filter": filter, "like": like, "in_": in_}
    return PageFilter(order_by, filter, like, in_)
